package com.baidu.shoppingmall.comment.service.impl;

import com.baidu.shoppingmall.comment.constant.CommentType;
import com.baidu.shoppingmall.comment.constant.ShowPictures;
import com.baidu.shoppingmall.comment.dao.CommentAggregateDAO;
import com.baidu.shoppingmall.comment.domain.CommentAggregatePO;
import com.baidu.shoppingmall.comment.domain.CommentInfoDTO;
import com.baidu.shoppingmall.comment.service.CommentAggregateService;
import com.baidu.shoppingmall.common.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;


/**
 * 评论统计信息管理模块的service
 * @author yjf
 *
 */
@Service
public class CommentAggregateServiceImpl extends ServiceImpl<CommentAggregateDAO,CommentAggregatePO> implements CommentAggregateService {

	private static final Logger logger = LoggerFactory.getLogger(CommentAggregateServiceImpl.class);

	/**
	 * 评论统计信息管理模块的DAO
	 */
	@Autowired
	private CommentAggregateDAO commentAggregateDAO;

	/**
	 * 更新评论统计信息
	 * @param commentInfoDTO 评论信息
	 * @return 处理结果
	 */
	@Override
	public CommentAggregatePO refreshCommentAggregate(CommentInfoDTO commentInfoDTO) {
		CommentAggregatePO commentAggregatePO = null;
		try {
            QueryWrapper<CommentAggregatePO> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = new HashMap<>();
            map.put("goods_id", commentInfoDTO.getGoodsId());
            //根据商品id查询出所有用户评论这个商品的信息
            commentAggregatePO = commentAggregateDAO.selectOne(queryWrapper);
			//当这个商品没被评论时
			if(commentAggregatePO == null) {
				//调用方法新增评论数据
				commentAggregatePO = saveCommentAggregate(commentInfoDTO);
			} else {
				//调用更新方法
				updateCommentAggregate(commentInfoDTO, commentAggregatePO);
			}
		} catch (Exception e) {
			logger.error("error", e);
			return null;
		}
		return commentAggregatePO;
	}

	/**
	 * 新增评论统计信息
	 * @param commentInfoDTO
	 */
	private CommentAggregatePO saveCommentAggregate(
			CommentInfoDTO commentInfoDTO) throws Exception {
		CommentAggregatePO commentAggregatePO = new CommentAggregatePO();

		//新增商品id
		commentAggregatePO.setGoodsId(commentInfoDTO.getGoodsId());
		//添加总评论数 因为是新增  所以添加一个1
		commentAggregatePO.setTotalCommentCount(1L);

		//给好评，中评，差评设置一个默认值（因为好评，中评还是差评只满足一个，数据库设计为不为null，所以要给另外两个设值）
		commentAggregatePO.setGoodCommentCount(0L);
		commentAggregatePO.setMediumCommentCount(0L);
		commentAggregatePO.setBadCommentCount(0L);

		//根据用户评论的类型添加  是好评，中评还是差评
		if(commentInfoDTO.getCommentType().equals(CommentType.GOOD_COMMENT)) {
			commentAggregatePO.setGoodCommentCount(1L);
		} else if(commentInfoDTO.getCommentType().equals(CommentType.MEDIUM_COMMENT)) {
			commentAggregatePO.setMediumCommentCount(1L);
		} else if(commentInfoDTO.getCommentType().equals(CommentType.BAD_COMMENT)) {
			commentAggregatePO.setBadCommentCount(1L);
		}

		//计算好评率
		Double goodCommentRate = Double.valueOf(new DecimalFormat("#.00").format(
				(double) commentAggregatePO.getGoodCommentCount() / (double) commentAggregatePO.getTotalCommentCount()));
		//添加好评率
		commentAggregatePO.setGoodCommentRate(goodCommentRate);

		//如果用户晒了图  就添加一个 晒图评论数
		commentAggregatePO.setShowPicturesCommentCount(0L);
		if(ShowPictures.YES.equals(commentInfoDTO.getShowPictures())) {
			commentAggregatePO.setShowPicturesCommentCount(1L);
		}

		//添加评论聚合统计信息的创建时间
		commentAggregatePO.setCreateTime(DateUtils.getCurrentTime());
		//原先没给这个商品评论  那么评论聚合统计信息的创建时间和修改时间应该一样
		commentAggregatePO.setUpdateTime(DateUtils.getCurrentTime());

		commentAggregateDAO.insert(commentAggregatePO);

		return commentAggregatePO;
	}

	/**
	 * 更新评论统计信息
	 * @param commentInfoDTO
	 * @param commentAggregatePO
	 */
	public void updateCommentAggregate(CommentInfoDTO commentInfoDTO,
										CommentAggregatePO commentAggregatePO) throws Exception {
		commentAggregatePO.setTotalCommentCount(commentAggregatePO.getTotalCommentCount() + 1L);
		//如果用户给的是好评  就在原有好评的基础上加一
		if(commentInfoDTO.getCommentType().equals(CommentType.GOOD_COMMENT)) {
			commentAggregatePO.setGoodCommentCount(commentAggregatePO.getGoodCommentCount() + 1L);
			//如果用户给的是中评
		} else if(commentInfoDTO.getCommentType().equals(CommentType.MEDIUM_COMMENT)) {
			commentAggregatePO.setMediumCommentCount(commentAggregatePO.getMediumCommentCount() + 1L);
			//如果用户给的是差评
		} else if(commentInfoDTO.getCommentType().equals(CommentType.BAD_COMMENT)) {
			commentAggregatePO.setBadCommentCount(commentAggregatePO.getBadCommentCount() + 1L);
		}

		//计算好评率
		Double goodCommentRate = Double.valueOf(new DecimalFormat("#.00").format(
				(double) commentAggregatePO.getGoodCommentCount() / (double) commentAggregatePO.getTotalCommentCount()));
		commentAggregatePO.setGoodCommentRate(goodCommentRate);

		//如果用户晒了图  就在原来晒图评论数的基础上加一
		if(ShowPictures.YES.equals(commentInfoDTO.getShowPictures())) {
			commentAggregatePO.setShowPicturesCommentCount(
					commentAggregatePO.getShowPicturesCommentCount() + 1L);
		}

		//添加评论聚合统计信息的修改时间
		commentAggregatePO.setUpdateTime(DateUtils.getCurrentTime());

		commentAggregateDAO.updateById(commentAggregatePO);
	}

}
